package com.easytouch.vmhdl.presenter;

import android.os.AsyncTask;

import com.easytouch.core.utils.Log;
import com.easytouch.vmhdl.interfaces.IManagerTestView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
 * Created by gulin on 2018/1/24.
 */

public class ManagerTestPresenter {

    private Log mLogger = new Log("ManagerTestPresenter.txt");

    private IManagerTestView mView;

    private boolean isStopped = true;

    String url1 = "easytouch-pay.com"; //正式服务器1 115.28.90.64
    String url2 = "easytouch-pay.cn"; //正式服务器2 120.27.26.183
    String url3 = "shop.easytouch-pay.cn"; //测试服务器 114.215.40.166
    String url4 = "easytouch-manager.cn"; //监控服务器 120.27.26.183

    int port1 = 6001;
    int port2 = 6001;
    int port3 = 6001;
    int port4 = 7007;

    boolean mBresult[] = new boolean[4];

    public ManagerTestPresenter(IManagerTestView pView)
    {
        mView = pView;
    }

    public void stopTest()
    {
        isStopped = true;
    }

    public void doTest()
    {
        isStopped = false;
        for (int i = 0;i < mBresult.length;i++)
        {
            mBresult[i] = false;
        }
        (new ConnectTask(url1, port1, 20, 1)).execute();
        (new ConnectTask(url2, port2, 20, 2)).execute();
        (new ConnectTask(url3, port3, 20, 3)).execute();
        (new ConnectTask(url4, port4, 20, 4)).execute();
    }

    private final class ConnectTask extends AsyncTask<Void, Integer, Void> {
        String url;
        int port;
        int count;
        int mNum;

        long mTime = 0L;
        int mTimeoutCount = 0;
        int mIOCount = 0;
        int mUnknownHostCount = 0;
        long mMinTime = 999999999;
        long mMaxTime = 0;

        public ConnectTask(String url, int port, int count, int num) {
            this.url = url;
            this.port = port;
            this.count = count;
            this.mNum =  num;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            if(isStopped)
            {
                return;
            }
            mView.updateProgress(mNum, values[0]);
        }

        @Override
        protected Void doInBackground(Void... params) {
            publishProgress(0);
            if(isStopped)
            {
                return null;
            }
            for(int i = 0;i < count;i++)
            {
                if(isStopped)
                {
                    return null;
                }
                Socket socket = null;
                InputStream in = null;
                OutputStream out = null;
                long time = System.currentTimeMillis();
                try {
                    socket = new Socket();
                    InetSocketAddress socAddress = new InetSocketAddress(url, port);
                    socket.connect(socAddress, 60000);
                    socket.setKeepAlive(true);
                    in = socket.getInputStream();
                    out = socket.getOutputStream();
//                    out.write(0x00);
//                    out.flush();
//                    byte[] read = new byte[128];
//                    in.read(read);

                } catch (UnknownHostException e) {
                    mUnknownHostCount++;
                } catch (SocketTimeoutException e) {// 超时
                    mTimeoutCount++;
                } catch (IOException e) {
                    mIOCount++;
                }
                finally {
                    // 关闭链接
                    try {
                        if (in != null)
                            in.close();
                        if (out != null)
                            out.close();
                        if (socket != null)
                            socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    long time1 = System.currentTimeMillis();
                    long time2 = time1 - time;
                    if(mMinTime > time2)
                        mMinTime = time2;
                    if(mMaxTime < time2)
                        mMaxTime = time2;
                    mTime += time2;
                }
                if(isStopped)
                {
                    return null;
                }
                double percent = (double)(i + 1) / (double)count * 100.0;
                publishProgress((int)percent);
            }
            mTime = mTime / count;
            publishProgress(100);
            return null;
        }

        @Override
        protected void onPostExecute(Void params) {
            super.onPostExecute(params);
            if(isStopped)
            {
                return;
            }
            double percent = (double)(count - mTimeoutCount - mIOCount - mUnknownHostCount) / (double)count * 100.0;
            mView.updateResult(mNum, count, percent, mTimeoutCount, mIOCount, mUnknownHostCount, mMinTime, mMaxTime, mTime);
            if(mNum - 1 < mBresult.length)
            {
                mBresult[mNum - 1] = true;
            }
            boolean isAllFinish = true;
            for (boolean isResult: mBresult) {
                if(!isResult)
                {
                    isAllFinish = false;
                    break;
                }
            }
            if(isAllFinish)
            {
                mView.finishTest();
            }
        }
    }
}
